﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using UniversalDataAccess.BusinessEntities;
using UniversalDataAccess;
using System.Configuration;
using UniversalDataAccess.DataAccess;

namespace WebApplication1
{
    /// <summary>
    /// Summary description for PersonsLogicService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class PersonsLogicService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        #region Select / Insert / Update /Delete Division.

        [WebMethod]
        public Person SelectPerson_By_DataBase_And_Id(string databaseName, int id)
        {
            try
            {
                PersonsDataAccess dataAccess = null;


                dataAccess = new PersonsDataAccess(databaseName, ConfigurationManager.ConnectionStrings[databaseName].ConnectionString, false);
                return dataAccess.SelectPersonBy_Id(id);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [WebMethod]
        public List<Person> SelectPersonListByDataBaseName(string dataBaseName)
        {
            try
            {
                PersonsDataAccess e = new PersonsDataAccess(dataBaseName, ConfigurationManager.ConnectionStrings[dataBaseName].ConnectionString, false);

                return e.SelectPersonsList();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [WebMethod]
        public void InsertPerson(Person person)
        {
            try
            {
                PersonsDataAccess e = new PersonsDataAccess(person.DataBaseName,
                    ConfigurationManager.ConnectionStrings[person.DataBaseName].ConnectionString, false);

                int new_Id = -1;
                e.InsertPerson(person, ref new_Id);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [WebMethod]
        public void UpdatePerson(Person person)
        {
            try
            {
                PersonsDataAccess e = new PersonsDataAccess(person.DataBaseName,
                    ConfigurationManager.ConnectionStrings[person.DataBaseName].ConnectionString, false);

                e.UpdatePerson(person);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        [WebMethod]
        public void DeletePerson(Person person)
        {
            try
            {
                PersonsDataAccess e = new PersonsDataAccess(person.DataBaseName,
                    ConfigurationManager.ConnectionStrings[person.DataBaseName].ConnectionString, false);

                e.DeletePerson(person);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion   
    }
}
